package com.liwie.app;
//Thread.State
//WAITING状态

//当前线程如果加入了其它线程（xx.join）,则xx线程必须执行完毕，当前线程才能继续执行，如果xx线程没有执行完毕，则当前线程WAITING
//当前线程加入当前线程，则死锁
public class MyApp2 {
    Thread thread1;
    Thread thread2;

    Runnable r1 = new Runnable() {
        @Override
        public void run() {
//            try {
//                thread2.join();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            for (int i = 1; i <= 10; i++) {
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + ":" + i);
//                if (i == 5) {
//                    try {
//                        thread2.join(1000);
//                    } catch (InterruptedException e) {
//                        throw new RuntimeException(e);
//                    }
//                }
            }

        }
    };
    Runnable r2 = new Runnable() {
        @Override
        public void run() {
//           //死锁
//            try {
//                thread2.join();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            for (int i = 201; i <= 210; i++) {

                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + ":" + i);
                if (i == 205) {
                    try {
                        thread2.join(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    };

    public static void main(String[] args) {
        MyApp2 myApp2 = new MyApp2();
        myApp2.thread1 = new Thread(myApp2.r1, "r1");
        myApp2.thread2 = new Thread(myApp2.r2, "r2");
        myApp2.thread1.start();
        myApp2.thread2.start();
        /*Runnable r1=new Runnable(){
            @Override
            public void run() {
                for (int i = 1; i <= 10; i++) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(i);
                }
            }
        };
        Runnable r2=new Runnable(){
            @Override
            public void run() {
                for (int i = 201; i <=210; i++) {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(i);
                }
            }
        };
        Thread thread = new Thread(r1, "r1");
        thread.start();
        Thread thread1 = new Thread(r2, "r2");
        thread1.start();*/
    }
}
